package com.feng.controller;

import com.feng.entity.User;
import com.feng.respository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @Value("${server.port}")
    private String port;

    @GetMapping("/findAll")
    public List<User> findAll(){
        return userRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") Integer id){
        Optional<User> user = userRepository.findById(id);
        return user.orElse(null);
    }

    @PostMapping("/save")
    public User save(@RequestBody User user){
        return userRepository.save(user);
    }

    @PutMapping("/update")
    public User update(@RequestBody User user){
        return userRepository.save(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        userRepository.deleteById(id);
    }

    @GetMapping("/index")
    public String index(){
        return "用户服务当前端口: " + this.port;
    }
}
